//
//  CustomTabBar.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/25.
//

#import "CustomTabBar.h"

@implementation CustomTabBar

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        [self setBackgroundImage:[UIImage new]];
        [self setShadowImage:[UIImage new]];
        self.layer.cornerRadius = 25;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat buttonW = self.frame.size.width / self.items.count;
    CGFloat buttonH = self.frame.size.height;
    CGFloat buttonY = 0;
    
    for (int index = 0; index < self.items.count; index++) {
        UIView *tabBarButton = self.subviews[index + 1];
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            CGFloat buttonX = index * buttonW;
            tabBarButton.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
            for (UIView *subview in tabBarButton.subviews) {
                if ([subview isKindOfClass:[UILabel class]]) {
                    subview.hidden = YES;
                }
            }
        }
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
        [NSObject clickFeedback];
        for (UIView *subview in view.subviews) {
            if ([subview isKindOfClass:[UIImageView class]]) {
                CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
                animation.values = @[@1.0, @0.8, @1.2, @1.0];
                animation.duration = 0.5;
                animation.calculationMode = kCAAnimationCubic;
                [subview.layer addAnimation:animation forKey:@"bounceAnimation"];
                break;
            }
        }
    }
    return view;
}

@end
